import DestructibleFacility from "../facility/DestructibleFacility";
import NodePoolManager from "../manager/NodePoolManager";

const { ccclass, property } = cc._decorator;

/**
 * 回收
 */
@ccclass
export default class FacilityRecycle extends cc.Component {

    fac: DestructibleFacility = null;

    init(fac: DestructibleFacility): void {
        this.fac = fac;
        this.fac.node.on(DestructibleFacility.EventType.摧毁, this.die, this);
    }

    die() {
        let price = this.fac.price;
        if (price) {
            let diamond = price.diamond ?? 0;
            let oil = price.oil ?? 0;
            this.fac.player.addDiamondAndOil(Math.ceil(diamond / 2), Math.ceil(oil / 2), this.fac.node.position);
        }
        this.fac.node.targetOff(this);
        this.fac = null;
        NodePoolManager.instance.recycle(this.node);
    }
}